package com.smedi.ismedi.progress.core.port.adapter.service.impl;

import com.smedi.ismedi.constructionproject.api.dto.request.ProjectListQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectBasicResponse;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectResp;
import com.smedi.ismedi.constructionproject.api.feign.RemoteConstructionProjectFeignApi;
import com.smedi.ismedi.progress.core.domain.dto.ProjectQuery;
import com.smedi.ismedi.progress.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.progress.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.progress.core.port.adapter.service.ProjectRemoteService;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class ProjectRemoteServiceImpl implements ProjectRemoteService {
    private final RemoteConstructionProjectFeignApi remoteConstructionProjectFeignApi;

    private final AdapterConverter adapterConverter;


    @Override
    public ProjectResp getProjectOne(ProjectQuery projectQuery) {
        return null;
    }

    @Override
    public List<ProjectResp> getProjectList(ProjectQuery projectQuery) {
        ProjectListQueryRequest projectListQueryRequest = adapterConverter.projectQueryToProjectListQueryRequest(projectQuery);
        ResponseData<List<ProjectResp>> responseData = remoteConstructionProjectFeignApi.getProjectList(projectListQueryRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        return responseData.getData();
    }

    @Override
    public List<ProjectBasicResponse> getAllProjectBasicList() {
        ResponseData<List<ProjectBasicResponse>> projectBasicResponse = remoteConstructionProjectFeignApi.getQueryAvailableProjectBasicList();
        if (!projectBasicResponse.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, projectBasicResponse.getApplication(), String.valueOf(projectBasicResponse.getErrorMsg()));
        }
        return projectBasicResponse.getData();
    }
}
